// pages/address/address.js
import {
  getAddressByUserIdAPI,
  deleteAddressAPI
} from '../../api/address'


Page({})

Component({
  data: {
    userId: 1,
    addressList: [],
    showDialog: false

  },
  methods: {
    onLoad(options) {
      let user = wx.getStorageSync('user')
      if (!user) {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }
      this.userId = user.id
      this.getAddressByUserId(this.userId)

    },
    onShow() {
      this.onLoad()
    },
    async getAddressByUserId(id) {
      const {
        result
      } = await getAddressByUserIdAPI(id);
      this.setData({
        addressList: result,
      })
    },
    handleEdit(e) {
      let id = e.currentTarget.dataset.index
      wx.navigateTo({
        url: `/pages/address/updateAddress/updateAddress?id=${id}`,
      })
    },
    handleDelete(e) {
      let id = e.currentTarget.dataset.index
      let that = this
      wx.showModal({
        title: '提示',
        content: '您确定要删除吗？',
        success: function (res) {
          if (res.confirm) {
            console.log(that);
            deleteAddressAPI(id)
            wx.pageScrollTo()
          } else if (res.cancel) {}
        }
      })
    },
    handleAdd(e) {
      wx.navigateTo({
        url: '/pages/address/addAddress/addAddress',
      })
    },
  }
})